import { useEffect, useRef, useState } from 'react'

/**
 *
 *
 * @param {*} fn 回调函数
 * @param {number} [ms=30] 时间间隔
 * @param {*} [deps=[]] // 依赖性数组
 * @return {*} 节流
 */
const useThrottle = (fn, ms = 30, deps = []) => {
  let previous = useRef(0)
  let [time, setTime] = useState(ms)
  useEffect(() => {
    let now = Date.now();
    if (now - previous.current > time) {
      fn();
      previous.current = now;
    }
  }, deps)

  const cancel = () => {
    setTime(0)
  }

  return [cancel]
}

export default useThrottle
